<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarProductListResponse extends AllAICSResponse
{

    public $isMain = true;

    public $index = 0;

    public function resolve(): Response
    {
        $body = $this->getBody();

        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));

        if ($body) {
            libxml_disable_entity_loader(false);
            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['head']['responseCompleteMessageStatus']['messageStatusCode'] ?? '';
            $message = $body['head']['responseCompleteMessageStatus']['messageStatusDescription'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $responseList = $this->getBody()['responseYwCvrg']['cvrgList'] ?? [];

        if ($this->isMain) {
            $this->printer = new NonCarProductListPrinter();
            foreach ($responseList as $item) {
                $productPrinter = new NonCarProductPrinter();
                $productPrinter->productName = $item['cInsuranceType'] ?? '';
                $productPrinter->productCode = $item['cInsuranceTypeCode'] ?? '';
                $productPrinter->productType = '驾意险';
                $productPrinter->planName = $item['cInsuranceType'] ?? '';
                $productPrinter->planCode = $item['cInsuranceTypeCode'] ?? '';
                $productPrinter->maxQuantity = $item['nPurchaseLimitsNum'] ?? '';

                $productPrinter->unitAmount = 0;
                $productPrinter->premium = 0;

                $this->printer->productList[$item['cInsuranceTypeCode']] = $productPrinter;
            }
            $this->isMain = false;
        } else {
            foreach ($responseList as $subs) {
                $subCoverage = new NonCarSubCoveragePrinter();
                $subCoverage->subCode = $subs['cInsuranceCode'] ?? '';
                $subCoverage->subName = $subs['cInsuranceLiability'] ?? '';
                $subCoverage->amount = $subs['nInsuranceAmountSeat'] ?? '';
                $subCoverage->premium = $subs['nLiabilityPremium'] ?? '';
                $subCoverage->rate = '';

                if ($subCoverage->amount) {
                    $subCoverage->amount .= ' /座/车';
                }

                $this->printer->productList[$subs['cInsuranceTypeCode']]->unitAmount += (int)$subCoverage->amount;
                $this->printer->productList[$subs['cInsuranceTypeCode']]->premium += $subCoverage->premium;

                $this->printer->productList[$subs['cInsuranceTypeCode']]->subCoverageAppend($subCoverage);
            }

            if (isset($subs)) {
                $this->printer->productList[$subs['cInsuranceTypeCode']]->unitAmount .= ' /座/车';
            }
        }

        return $this;
    }
}
